﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using Scoreboard.Interfaces;
using Scoreboard.Model;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using Scoreboard.Model.Services;

namespace Scoreboard.Repository
{
	public class UserRepository : Repository, IUserRepository
	{
		private IEnumerable<User> _preloadedUsers = new List<User>();

		public UserRepository(ScoreboardDomainContext context) : base(context)
		{
		}

		public void PreloadUsers()
		{
			GetUsers(PreloadUsersCompletedHandler);
		}

		public IEnumerable<User> GetPreloadedUsers()
		{
			return _preloadedUsers;
		}

		public void GetUsers(Action<LoadOperation<User>> callback)
		{
			Context.Load(Context.GetUsersQuery(), LoadBehavior.RefreshCurrent, callback, null);
		}

		public void GetRatingTable(Action<LoadOperation<User>> callback)
		{			
			Context.Load(Context.GetRatingTableQuery(), LoadBehavior.RefreshCurrent, callback, null);
		}		

		public void Register(Action<InvokeOperation> callback)
		{
			Context.Register(callback, null);
		}

		private void PreloadUsersCompletedHandler(LoadOperation<User> operation)		
		{
			_preloadedUsers = operation.Entities.ToList();
		}
	}
}
